package class01;

import java.util.Arrays;
import java.util.IllegalFormatCodePointException;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class e5 {

    public static int nearestRight(int[] arr,int value){
        int L=0;
        int R=arr.length-1;
        int index=-1;
        while (L<=R){
            int mid = L+((R-L)>>1);
            if (arr[mid]<=value){
                index=mid;
                L = mid+1;
            }else {
                R=mid-1;
            }
        }
        return index;
    }

    public static int test(int[] arr,int value){
        for (int i=arr.length-1;i>=0;i--){
            if (arr[i]<=value)
                return i;
        }
        return -1;
    }

    public static int[] generateRandomArray(int maxSize,int maxValue){
        int[] arr = new int[(int)((maxSize+1)*Math.random())];
        for (int i=0;i<arr.length;i++){
            arr[i] = (int)((maxValue+1)*Math.random())-(int)((maxValue+1)*Math.random());

        }
        return arr;
    }

    public static void printArray(int[] arr){
        if (arr==null){
            return;
        }
        for (int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int testTime =500000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed=true;

        for (int i=0;i<testTime;i++){
            int[] arr=generateRandomArray(maxSize,maxValue);
            Arrays.sort(arr);
            int value = (int)((maxValue+1)*Math.random())-(int)((maxValue+1)*Math.random());
            if (test(arr,value)!=nearestRight(arr,value)){
                printArray(arr);
                succeed=false;
                break;
            }
        }
        System.out.println(succeed?"Nice!":"Fucking fucked!");
    }
}
